package com.jhgsys.internal.base.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.base.entity.BaseUserScoreRecord;
import com.jhgsys.internal.base.service.IBaseUserScoreRecordService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 用户积分记录 Controller
 *
 * @author admin
 * @date 2024-09-04 13:07:28
 */
@Slf4j
@Validated
@Controller
public class BaseUserScoreRecordController extends BaseController {

    @Autowired
    private IBaseUserScoreRecordService baseUserScoreRecordService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseUserScoreRecord")
    @RequiresPermissions("baseUserScoreRecord:view")
    public String baseUserScoreRecordIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("totalScore", baseUserScoreRecordService.findTotalScoreByUserId(getCurrentUserId()));
        return FebsUtil.view("base/baseUserScoreRecord/baseUserScoreRecordList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseUserScoreRecord/add")
    @RequiresPermissions("baseUserScoreRecord:add")
    public String baseUserScoreRecordAdd(){
        return FebsUtil.view("base/baseUserScoreRecord/baseUserScoreRecordEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseUserScoreRecord/update/{id}")
    @RequiresPermissions("baseUserScoreRecord:edit")
    public String baseUserScoreRecordUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("baseUserScoreRecord", baseUserScoreRecordService.findBaseUserScoreRecordById(id));
        return FebsUtil.view("base/baseUserScoreRecord/baseUserScoreRecordEdit");
    }

    /**
     * 用户积分页
     * @param model
     * @return
     */
    @GetMapping(FebsConstant.VIEW_PREFIX + "userScore")
    @RequiresPermissions("userScore:view")
    public String systemUser(Model model) {
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("tenantList",getTenantList());
        model.addAttribute("currentTenantId", getCurrentTenantId());
        return FebsUtil.view("base/userScore/userScore");
    }

    /**
     * 用户积分明细页
     * @return
     */
    @GetMapping(FebsConstant.VIEW_PREFIX + "userScoreDetail")
    @RequiresPermissions("userScoreDetail:view")
    public String userScoreDetail(){
        return FebsUtil.view("base/userScore/userScoreDetail");
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-09-04 13:07:28
     */
    @GetMapping("baseUserScoreRecord")
    @ResponseBody
    @RequiresPermissions("baseUserScoreRecord:view")
    public FebsResponse getAllBaseUserScoreRecords(BaseUserScoreRecord baseUserScoreRecord) {
        return new FebsResponse().success().data(baseUserScoreRecordService.findBaseUserScoreRecordList(baseUserScoreRecord));
    }

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-09-04 13:07:28
     */
    @GetMapping("baseUserScoreRecord/list")
    @ResponseBody
    @RequiresPermissions("baseUserScoreRecord:view")
    public FebsResponse baseUserScoreRecordList(QueryRequest request, BaseUserScoreRecord baseUserScoreRecord) {
        if (StringUtils.isBlank(baseUserScoreRecord.getUserId())) {
            baseUserScoreRecord.setUserId(getCurrentUserId());
        }
        Map<String, Object> dataTable = getDataTable(this.baseUserScoreRecordService.findBaseUserScoreRecordPage(request, baseUserScoreRecord));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 BaseUserScoreRecord
     * @author admin
     * @date 2024-09-04 13:07:28
     */
    @Log("新增BaseUserScoreRecord")
    @PostMapping("baseUserScoreRecord")
    @ResponseBody
    @RequiresPermissions("baseUserScoreRecord:add")
    public FebsResponse addBaseUserScoreRecord(@Valid BaseUserScoreRecord baseUserScoreRecord) throws FebsException {
        try {
            baseUserScoreRecord.setCreatedBy(getCurrentUser().getUserName());
            this.baseUserScoreRecordService.createBaseUserScoreRecord(baseUserScoreRecord);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增BaseUserScoreRecord失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 BaseUserScoreRecord
     * @author admin
     * @date 2024-09-04 13:07:28
     */
    @Log("逻辑删除BaseUserScoreRecord")
    @GetMapping("baseUserScoreRecord/delete/{id}")
    @ResponseBody
    @RequiresPermissions("baseUserScoreRecord:delete")
    public FebsResponse deleteBaseUserScoreRecord(@PathVariable("id") String baseUserScoreRecordIds) throws FebsException {
        try {
            BaseUserScoreRecord baseUserScoreRecord = new BaseUserScoreRecord();
            baseUserScoreRecord.setDeletedBy(getCurrentUser().getUserName());
            baseUserScoreRecord.setDeletedTime(new Date());
            baseUserScoreRecord.setDeletedFlg(FebsConstant.INVALID);
            this.baseUserScoreRecordService.deleteBaseUserScoreRecord(baseUserScoreRecord,baseUserScoreRecordIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseUserScoreRecord失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 BaseUserScoreRecord
    * @author admin
    * @date 2024-09-04 13:07:28
    */
    @Log("物理删除BaseUserScoreRecord")
    @GetMapping("baseUserScoreRecord/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("baseUserScoreRecord:delete")
    public FebsResponse physicallyDeleteBaseUserScoreRecord(@PathVariable("id") String baseUserScoreRecordIds) throws FebsException {
        try {
            this.baseUserScoreRecordService.physicallyDeleteBaseUserScoreRecord(baseUserScoreRecordIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseUserScoreRecord失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 BaseUserScoreRecord
     * @author admin
     * @date 2024-09-04 13:07:28
     */
    @Log("修改BaseUserScoreRecord")
    @PostMapping("baseUserScoreRecord/update")
    @ResponseBody
    @RequiresPermissions("baseUserScoreRecord:edit")
    public FebsResponse updateBaseUserScoreRecord(BaseUserScoreRecord baseUserScoreRecord) throws FebsException {
        try {
            baseUserScoreRecord.setUpdatedBy(getCurrentUser().getUserName());
            this.baseUserScoreRecordService.updateBaseUserScoreRecord(baseUserScoreRecord);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改BaseUserScoreRecord失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
